﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Keepo.Models;
using AlbumDesigner.Helpers;
using System.Web.SessionState;

namespace AlbumDesigner.Controllers
{
    [SessionState(SessionStateBehavior.Disabled)] // SessionLess - useful for long actions, to allow other requests to be made at this time
    public class RenderController : Controller
    {
        private AlbumContext db = new AlbumContext();

        public ActionResult Index()
        {
            var serverIp = Request.ServerVariables["LOCAL_ADDR"];
            var clientIp = Request.ServerVariables["REMOTE_ADDR"];

            return View();

            //if the request came from the Server
            // if (serverIp == clientIp)
            // {
            //    return View();
            // }
            // else
            // {
            //    return View("NoPermission");
            // }
        }

        public ActionResult IsRendering()
        {
            if(db.RenderTasks.Count() > 0)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }

        }

        public ActionResult GetRenderTasks()
        {
            //update the last run of Render Check
            var renderVariables = db.RenderVariables.FirstOrDefault();
            if (renderVariables != null)
            {
                renderVariables.LastCheck = DateTime.Now;
            }

            var renderTask = db.RenderTasks.FirstOrDefault(m => m.Started == false);

            //if found task to render - return it to the View, javascript will open the Flex album render proccess
            if (renderTask != null)
            {
                renderTask.Started = true;
            }

            db.SaveChanges();

            return Json(renderTask, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// This action is being called from Flex, right after the Render of the album pages complete
        /// This action start the 2nd proccess - Pdf Generation
        /// </summary>
        [HttpPost]
        public ActionResult AlbumRenderComplete(int albumId)
        {
            var renderTask = db.RenderTasks.FirstOrDefault(m => m.Started);

            //find the render task that has been started
            if (renderTask != null)
            {
                var id = renderTask.AlbumId;
                var quality = renderTask.Quality;

                PdfHelper.GeneratePDF(id, quality);
            }

            return Json(true);
        }

        // Invoke Manually 
        public ActionResult GeneratePdf(int id, int quality)
        {
            var result = PdfHelper.GeneratePDF(id, quality);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        // Invoke Manually
        // public ActionResult GetFullImageForAlbumPage(int id)
        // {
        //    var bitmap = PdfHelper.GetFullImageForAlbumPage(id);
        //    var contentType = "image/jpeg";
        //
        //    Byte[] ba = EncodeHelper.EncodeBitmap(bitmap, contentType);
        //    return File(ba, contentType);
        //}


    }
}
